


/**
 * primitive model 大量加载
 * 
 */
import Primitive from "../Primitive"

const DEFAULT_STYLE = {
  scale : 1,
  shadows : Cesium.ShadowMode.DISENABLED
}

export default class ModelPrimitive extends Primitive{
  /**
   * 
   * @param {*} position 
   * @param {*} modelUrl 
   */
  constructor(positions , modelUrl , options = {}){
    super()
    this._positions = positions
    this._modelUrl = modelUrl
    this._type = 'model_primitive'
    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }

    this.delegate = Cesium.Model.fromGltf({
      url : this._modelUrl
    })

    this.delegate.readyPromise.then(model => {
      model.activeAnimations.addAll({
        multiplier: 0.5,
        loop: Cesium.ModelAnimationLoop.REPEAT
      })
    })

    this.mounted()
  }

  mounted(){
    this.modelUrl = this._modelUrl
    this.positions = this._positions
    this.style = this._style
  }

  get readyPromise(){
    return this.delegate.readyPromise
  }

  get modelUrl(){
    return this._modelUrl
  }

  set modelUrl(val){
    this._modelUrl = val
    this._delegate = Cesium.Model.fromGltf({url: val})
  }

  get positions(){
    return this._positions
  }

  set positions(val){
    this._positions = val
    this.delegate.modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
      this._positions,
      new Cesium.HeadingPitchRoll(
        Cesium.Math.toRadians(this.heading),
        Cesium.Math.toRadians(this.pitch),
        Cesium.Math.toRadians(this.roll)
      )
    )
  }
  /**
   * 
   * @param {*} style 
   * @returns 
   */

  setStyle(style){
    if(!style) return
    Object.assign(this._style , {...style})
    Object.assign(this.delegate , {
      ...this._style
    })
  }
}
 